package com.liuyjy.base.except.config;

import com.liuyjy.base.common.result.Result;
import com.liuyjy.base.common.result.ResultCodeEnum;
import com.liuyjy.base.except.exception.AppException;
import com.liuyjy.base.except.exception.BaseException;
import com.liuyjy.base.except.exception.RemoteRpcException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @Author liuyjy
 * @Date 2022/2/8 14:28
 * @Description: Remote 统一异常
 **/
@Slf4j
@ControllerAdvice
@ResponseBody
@ConditionalOnProperty(prefix = "common.setting.except", name = "type", havingValue = "remote")
public class RemoteExceptionHandler {

    //处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常，详情继续往下看代码
//    @ExceptionHandler(BindException.class)
//    @ResponseBody
//    public Result bindExceptionHandler(BindException e) {
//        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
//        return Result.generate(ResultCodeEnum.SYSTEM_ERROR.code,message);
//    }

    //处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是javax.validation.ConstraintViolationException
//    @ExceptionHandler(ConstraintViolationException.class)
//    @ResponseBody
//    public Result constraintViolationExceptionHandler(ConstraintViolationException e) {
//        String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
//        return Result.generate(ResultCodeEnum.SYSTEM_ERROR.code,message);
//    }

    //处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常。
//    @ExceptionHandler(MethodArgumentNotValidException.class)
//    @ResponseBody
//    public Result methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
//        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
//        return Result.generate(ResultCodeEnum.SYSTEM_ERROR.code,message);
//    }

/*    @ExceptionHandler({BindException.class, ValidationException.class, MethodArgumentNotValidException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result bindExceptionHandler(Exception e) {
        log.error("参数验证异常！原因是:", e);
        return ValidationUtil.createValidMsgOld(e);
    }*/


    /**
     * 拦截自定义异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler({BaseException.class, RemoteRpcException.class, AppException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result exceptionHandler(BaseException ex) {
        log.error("BaseException:{}", ex.toString());
        return Result.generate(ex.getCode(), ex.getMsg());
    }

    /**
     * 拦截所有错误
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public Result exHandler(Exception ex) {
        log.error("系统错误:", ex);
        return Result.generate(ResultCodeEnum.SYSTEM_ERROR);
    }


}
